package com.moyuxi.sound_of_the_forest.ui.pages

import android.Manifest
import android.os.Build
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.absoluteOffset
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.navigation.NavHostController
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.PermissionStatus
import com.google.accompanist.permissions.rememberPermissionState
import com.moyuxi.sound_of_the_forest.viewModel.MainViewModel

@RequiresApi(Build.VERSION_CODES.Q)
@OptIn(ExperimentalPermissionsApi::class, ExperimentalMaterial3Api::class)
@Composable
fun ScanMusicScreen(mainNC: NavHostController, mainVM: MainViewModel) {
    //申请当前需要的权限
    val permissionState = rememberPermissionState(
        Manifest.permission.READ_EXTERNAL_STORAGE
    )
    val context = LocalContext.current
    val openDialog = remember { mutableStateOf(false) }


    Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
        CenterAlignedTopAppBar(
            title = { Text(text = "扫描歌曲") },
            navigationIcon = {
                Icon(Icons.Default.ArrowBack,
                    "返回",
                    modifier = Modifier
                        .size(36.dp)
                        .clickable { mainNC.popBackStack() })
            })
        Button(onClick = {
            openDialog.value = true
            when (permissionState.status) {
                is PermissionStatus.Denied -> {
                    val textToShow =
                        if ((permissionState.status as PermissionStatus.Denied).shouldShowRationale) "扫描音乐需要存储权限."
                        else "存储权限对扫描音乐是必需的, 请授予权限."
                    Toast.makeText(context, textToShow, Toast.LENGTH_SHORT).show()
                    permissionState.launchPermissionRequest()
                }
                //已授权
                PermissionStatus.Granted -> {
                    mainVM.loadLocalMusicData(context)
                    Toast.makeText(context, "扫描完成。", Toast.LENGTH_SHORT).show()
                }
            }
            openDialog.value = false;
        }) {
            Text(text = "开始扫描！")
        }


    }

    if (openDialog.value) {
        Dialog(onDismissRequest = { }) {
            Card(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(150.dp)
                    .padding(16.dp),
                shape = RoundedCornerShape(16.dp),
            ) {
                Column(
                    Modifier.fillMaxWidth(),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.SpaceBetween
                ) {
                    Spacer(modifier = Modifier.height(16.dp))
                    CircularProgressIndicator(modifier = Modifier.fillMaxWidth(0.15f))
                    Text(
                        text = "正在扫描中……",
                        modifier = Modifier.absoluteOffset {
                            IntOffset(
                                0.dp.roundToPx(), 30.dp.roundToPx()
                            )
                        },
                        textAlign = TextAlign.Center,
                    )
                }
            }
        }
    }
}